/*
*@module: projectInfo
*@author: 陈刚
*/


const AllProjectDataModel = require('../../models/table/projectInfo.js')
const AllMap = require('../../models/common/Map.js')
const jwt = require('jsonwebtoken')
const secret = 'shu-project'
var areaMap
var element_Map
AllMap.then(data => {
  areaMap = data.area_Map
  element_Map = data.element_Map
})

// const areaMap = new Map([['0001', '静安区'], ['0002', '长宁区'], ['0003','宝山区']])

class ProjectInfo_Controller {

  /**
   *获取对应用户权限的项目三级选择器
   * @param ctx token  分发的用户token
   *
   * @memberof ProjectInfo_Controller
   */
  static async getProjectInfo(ctx) {

    const token = ctx.header['shu-token']
    let roles
    let project_owner
    if (token) {
      try {
        let playload = await jwt.verify(token, secret)
        console.log('playload', playload)
        roles = playload.roles
        project_owner = playload.project_owner.split('、')
      } catch (err) {
        console.log(err)
      }
    } else {
      return
    }

    let allProjectData1 = await AllProjectDataModel.getAllProjectData(roles, project_owner)
    let AllProjectData = allProjectData1.map(item => {
      return item = item.dataValues
    })
    let project_obj = {}
    let project_arr = []
    AllProjectData.forEach((item, index) => {
      if (item.project_area in project_obj) {
        project_obj[item.project_area].push({
          value: item.project_num,
          label: item.project_name
        })
      } else {
        project_obj[item.project_area] = []
        project_obj[item.project_area].push({
          value: item.project_num,
          label: item.project_name
        })
      }
    })
    // console.log('project_obj', project_obj)
    // console.log('areaMap', areaMap)
    for (let key in project_obj) {
      project_arr.push({
        value: key,
        label: areaMap.get(key),
        children: project_obj[key]
      })
    }
    // console.log('project_arr', project_arr)
    if (AllProjectData) {

      ctx.body = {
        success: true,
        res: project_arr,
        msg: '获取成功'
      }
    } else {
      ctx.body = {
        success: false,
        msg: '获取失败'
      }
    }
  }

  /**
   *获取某项目体下所有的监测点位列表（供监测点位筛选）
   * @param ctx project_num  项目体编号
   *
   * @memberof ProjectInfo_Controller
   */
  static async getPointOptions(ctx) {
    let project_num = ctx.params.project_num
    let point = await AllProjectDataModel.getPoint(project_num)
    let PointOptions = point.map(item => {
      return item = { value: item.dataValues.point_num, label: item.dataValues.remarks }
    })
    console.log('PointOptions', PointOptions)
    // console.log('PointOptions', PointOptions)
    PointOptions.unshift({ value: '0', label: '全部' })

    if (point) {
      ctx.body = {
        success: true,
        res: PointOptions,
        msg: '获取成功'
      }
    } else {
      ctx.body = {
        success: false,
        msg: '获取失败'
      }
    }
  }

  /**
   *获取某项目体下所有监测点位的坐标位置
   * @param ctx project_num 项目体编号
   *
   * @memberof ProjectInfo_Controller
   */
  static async getMarkerList(ctx) {
    let project_num = ctx.params.project_num
    let pointMarkerList = await AllProjectDataModel.getPointMarkerList(project_num)
    let MarkerList = pointMarkerList.map(item => {
      return item = item.dataValues
    })

    if (pointMarkerList) {
      ctx.body = {
        success: true,
        res: MarkerList,
        msg: "获取坐标成功"
      }
    } else {
      ctx.body = {
        success: false,
        res: [],
        msg: "获取坐标失败"
      }
    }
  }

  static async getElementOptions(ctx) {
    let project_num = ctx.params.project_num
    let elementOptions_list = await AllProjectDataModel.getElementOptions_list(project_num)
    console.log('elementOptions_list',elementOptions_list)
    let element_list = elementOptions_list? elementOptions_list.dataValues : {}
    console.log('element_list', element_list)
    let elementOptions = [{ value: 'all', label: '全部' }]
    for (let key in element_list) {
      let item = element_list[key]
      if (item != '-1') {
        elementOptions.push({ value: key, label: element_Map.get(key) })
      }
    }

    if (elementOptions_list) {
      ctx.body = {
        success: true,
        res: elementOptions,
        msg: "获取元素列表成功"
      }
    } else {
      ctx.body = {
        success: false,
        res: [],
        msg: "获取元素列表失败"
      }
    }
  }


  static async getR2List(ctx) {
    let body = ctx.request.body
    console.log('body', body)
    // let selectedProject = body.selectedProject
    let R2List = await AllProjectDataModel.getR2List(body)
    // console.log('R2List',R2List)
    let r2List_all = R2List.map(item => {
      return item = item.dataValues
    })
    console.log('r2List_all', r2List_all)
    let assess_type_map = ['初步调查', '详细调查', '修复调查']

    let result = []
    r2List_all.forEach(item => {
      for (let key in item) {
        if (element_Map.has(key)) {
          let assess_type_CN = assess_type_map[item['assess_type'] - 1]
          result.push({
            project_name: item['project_name'],
            assess_type: item['assess_type'],
            assess_type_CN: assess_type_CN,
            element: key,
            element_CN: element_Map.get(key),
            r2: item[key],
            isAccept: parseFloat(item[key]) >= 0.95 ? 1 : 0
          })
        }
      }

    })

    // console.log('result', result)
    if (R2List) {
      ctx.body = {
        success: true,
        res: result,
        msg: "获取r2列表成功"
      }
    } else {
      ctx.body = {
        success: false,
        res: [],
        msg: "获取r2列表失败"
      }
    }
  }
}

module.exports = ProjectInfo_Controller
